package com.caipiao.service.impl;

import com.caipiao.mapper.SMemberMapper;
import com.caipiao.mapper.SPlanMapper;
import com.caipiao.mapper.SUserMemberMapper;
import com.caipiao.pojo.*;
import com.caipiao.service.MemberService;
import com.caipiao.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.dc.pr.PRError;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * Created by Administrator on 2019/1/8.
 */
@Service
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private SMemberMapper sMemberMapper;
    @Autowired
    private SPlanMapper sPlanMapper;
    @Autowired
    private SUserMemberMapper sUserMemberMapper;

    @Override
    public ObjectResult     memberFollow(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());

        List<SMember> list = new ArrayList<>();

        if ("1".equals(xmlBean.getType())){
            list = sMemberMapper.selectMemberByUserId(Integer.parseInt(userId));
        }else {
            list = sMemberMapper.selectMember(null);
        }

        Page<SMember> page = new Page<>();
        page.setP(xmlBean.getP()==null? 0 :xmlBean.getP());
        List<SMember> pageContent = page.getPageContent(list);

        for (SMember sMember:pageContent){
            sMember.setList(sMemberMapper.selectMemberLeague(sMember.getId()));
            sMember.setHead(ConfigUtils.ADMIN_URL+sMember.getHead());
            //
            Map<String, Object> data = new HashMap<>();
            data.put("memberId",sMember.getId());
            data.put("matchState","(1)");
            List<SPlan> list1 = sPlanMapper.selectplantByMemberId(data);//最新方案
            sMember.setPlanCount(list1.size());

            //是否关注
            SUserMember member = new SUserMember();
            member.setUserId(Integer.parseInt(userId));
            member.setMemberId(sMember.getId());
            Integer isFollow = sUserMemberMapper.select(member);
            if (isFollow == null){
                sMember.setIsFollow(0);
            }else {
                sMember.setIsFollow(isFollow);
            }

            //命中率
            int count = 0;
            data.put("matchState","(3)");
            List<SPlan> list2 = sPlanMapper.selectplantByMemberId(data);
//            if(list2.size()>0) {
//                for (int i = 0; i < list2.size(); i++) {
//                    if (list2.get(i).getIsWin() == 1) {
//                        count++;
//                    }
//                }
//
//                BigDecimal divide = new BigDecimal(count * 100).divide(new BigDecimal(list2.size()),0, RoundingMode.HALF_UP);
//
//                sMember.setHitRate(divide );
//            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("count",page.getPageTotal());
        map.put("list",pageContent);
        return ObjectResult.build("1","成功",map);
    }

    @Override
    public ObjectResult memberPlan(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());
        List<SPlan> list = sPlanMapper.selectplantByUserId(Integer.parseInt(userId));

        Page<SPlan> page = new Page<>();
        page.setP(xmlBean.getP()==null? 0 :xmlBean.getP());
        List<SPlan> pageContent = page.getPageContent(list);

        for (SPlan sPlan:pageContent){
            sPlan.setTime(RelativeDateFormat.data(sPlan.getAddTime()));
            sPlan.setHead(ConfigUtils.ADMIN_URL+sPlan.getHead());
        }

        Map<String,Object> map = new HashMap<>();
        map.put("count",page.getPageTotal());
        map.put("list",pageContent);

        return ObjectResult.build("1","成功",map);
    }

    @Override
    public ObjectResult memberDetails(XMLBean xmlBean) {
        SMember sMember = sMemberMapper.selectByPrimaryKey(Integer.parseInt(xmlBean.getId()));

        //是否关注
        if(!StringUtil.isEmpty(xmlBean.getToken())){
            String userId = CommonUtils.decToken(xmlBean.getToken());
            SUserMember member = new SUserMember();
            member.setUserId(Integer.parseInt(userId));
            member.setMemberId(sMember.getId());
            Integer isFollow = sUserMemberMapper.select(member);
            if (isFollow == null){
                sMember.setIsFollow(0);
            }else {
                sMember.setIsFollow(isFollow);
            }
        }else {
            sMember.setIsFollow(0);
        }
        //擅长的联赛
        sMember.setList(sMemberMapper.selectMemberLeague(sMember.getId()));
        sMember.setHead(ConfigUtils.ADMIN_URL+sMember.getHead());

        Map<String, Object> map = new HashMap<>();
        map.put("memberId",sMember.getId());
        map.put("matchState","(1)");
        List<SPlan> list1 = sPlanMapper.selectplantByMemberId(map);//最新方案
        for(SPlan sPlan:list1){
            sPlan.setTime(RelativeDateFormat.data(sPlan.getAddTime()));
        }

        map.put("matchState","(2,3,4,5)");
        List<SPlan> list2 = sPlanMapper.selectplantByMemberId(map);//历史方案
        Page<SPlan> page = new Page<>();
        page.setP(xmlBean.getP()==null ? 1:xmlBean.getP());
        List<SPlan> pageContent = page.getPageContent(list2);

        for (SPlan sPlan:list2){
            sPlan.setTime(RelativeDateFormat.data(sPlan.getAddTime()));
        }

        map.clear();
        map.put("member",sMember);
        map.put("list1",list1);
        map.put("list2",pageContent);
        map.put("count",page.getPageTotal());
        return ObjectResult.build("1","成功",map);
    }

    @Override
    public ObjectResult foller(XMLBean xmlBean) {
        String userId = CommonUtils.decToken(xmlBean.getToken());

        SUserMember sUserMember = new SUserMember();
        sUserMember.setMemberId(Integer.parseInt(xmlBean.getId()));
        sUserMember.setUserId(Integer.parseInt(userId));
        Integer select = sUserMemberMapper.select(sUserMember);

        SMember sMember = sMemberMapper.selectByPrimaryKey(Integer.parseInt(xmlBean.getId()));

        if(select == null){
            sUserMember.setIsFollow(1);
            sUserMemberMapper.insertSelective(sUserMember);
            //修改粉丝数
            sMember.setFans(sMember.getFans()+1);
            sMemberMapper.updateByPrimaryKeySelective(sMember);

            return ObjectResult.build("1","关注成功");
        }else if(select ==0) {
            sUserMember.setIsFollow(1);
            sUserMemberMapper.update(sUserMember);
            //修改粉丝数
            sMember.setFans(sMember.getFans()+1);
            sMemberMapper.updateByPrimaryKeySelective(sMember);

            return ObjectResult.build("1","关注成功");
        }else {
            sUserMember.setIsFollow(0);
            sUserMemberMapper.update(sUserMember);
            //修改粉丝数
            sMember.setFans(sMember.getFans()-1);
            sMemberMapper.updateByPrimaryKeySelective(sMember);

            return ObjectResult.build("1","取消成功");
        }
    }

    @Override
    public ObjectResult memberList(XMLBean xmlBean) {
        if("1".equals(xmlBean.getType())){
            List<SMember> sMembers = sMemberMapper.selectMember(ConfigUtils.MEMBER_ID);
            return ObjectResult.build("1","成功",sMembers);
        }
        if("2".equals(xmlBean.getType())){
            List<Map<String,Object>> list = new LinkedList<>();
            Map<String,Object> map1 = new HashMap<>();
            map1.put("title","单关");
            map1.put("type","2");
            list.add(map1);
            Map<String,Object> map2 = new HashMap<>();
            map2.put("title","二串一");
            map2.put("type","3");
            list.add(map2);
            Map<String,Object> map3 = new HashMap<>();
            map3.put("title","总进球");
            map3.put("type","4");
            list.add(map3);

            return ObjectResult.build("1","成功",list);
        }

        return null;
    }
}
